﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class CartonDA : ICartonDA
    {
        private string CARTON_GROUP = "@CARTON_GROUP";
        private string CARTON_ID = "@CARTON_ID";
        private string CARTON_TYPE = "@CARTON_TYPE";
        private string CREATED_BY = "@CREATED_BY";
        private string CREATED_DATE = "@CREATED_DATE";
        private string CUBE = "@CUBE";
        private string DESCRIPTION = "@DESCRIPTION";
        private string HEIGHT = "@HEIGHT";
        private string IS_ACTIVE = "@IS_ACTIVE";
        private string LENGTH = "@LENGTH";
        private string MAX_COUNT = "@MAX_COUNT";
        private string MAX_WEIGHT = "@MAX_WEIGHT";
        private string SEQ_NO = "@SEQ_NO";
        private string SQL_CHECK_Carton_ID_UNIQUE = " SELECT COUNT(1) FROM @_@CARTON WHERE ";
        private string SQL_DELETE_Carton = " DELETE @_@CARTON WHERE CARTON_ID = @CARTON_ID AND SYSTEM_CREATED = 'N'  ";
        private string SQL_INSERT_Carton = " INSERT INTO @_@CARTON ( WH_ID, CARTON_ID, CARTON_GROUP, CARTON_TYPE, DESCRIPTION, SEQ_NO, CUBE, MAX_WEIGHT, MAX_COUNT , LENGTH , WIDTH , HEIGHT , CREATED_BY , CREATED_DATE , UPDATED_BY , UPDATED_DATE , IS_ACTIVE , SYSTEM_CREATED) VALUES(@WH_ID, @CARTON_ID, @CARTON_GROUP, @CARTON_TYPE, @DESCRIPTION, @SEQ_NO, @CUBE, @MAX_WEIGHT, @MAX_COUNT , @LENGTH , @WIDTH , @HEIGHT , @CREATED_BY , @CREATED_DATE , @UPDATED_BY , @UPDATED_DATE , @IS_ACTIVE , @SYSTEM_CREATED)";
        private string SQL_SELECT_ALL_Carton = " SELECT  ROWID, WH_ID, CARTON_ID, CARTON_GROUP, CARTON_TYPE, DESCRIPTION, SEQ_NO, CUBE, MAX_WEIGHT, MAX_COUNT , LENGTH , WIDTH , HEIGHT , CREATED_BY , CREATED_DATE , UPDATED_BY , IS_ACTIVE,UPDATED_DATE , SYSTEM_CREATED FROM  @_@CARTON ";
        private string SQL_UPDATE_Carton = " UPDATE @_@CARTON SET WH_ID = @WH_ID, CARTON_GROUP = @CARTON_GROUP, CARTON_TYPE = @CARTON_TYPE , DESCRIPTION = @DESCRIPTION , SEQ_NO = @SEQ_NO , CUBE = @CUBE , MAX_WEIGHT = @MAX_WEIGHT , MAX_COUNT = @MAX_COUNT , LENGTH = @LENGTH , WIDTH = @WIDTH , HEIGHT = @HEIGHT , UPDATED_DATE = @UPDATED_DATE, UPDATED_BY = @UPDATED_BY , IS_ACTIVE = @IS_ACTIVE , SYSTEM_CREATED = @SYSTEM_CREATED WHERE CARTON_ID = @CARTON_ID  ";
        private string SYSTEM_CREATED = "@SYSTEM_CREATED";
        private string UPDATED_BY = "@UPDATED_BY";
        private string UPDATED_DATE = "@UPDATED_DATE";
        private string WH_ID = "@WH_ID";
        private string WIDTH = "@WIDTH";

        public bool CheckCartonUnique(string cartonID, string whLoginID)
        {
            string strSql = this.SQL_CHECK_Carton_ID_UNIQUE.Replace("@_@", whLoginID) + " CARTON_ID = @CARTON_ID";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CARTON_ID", cartonID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, strSql, paras).ToString() == "1");
        }

        public bool CheckCartonUnique(DataBase dataBase, DbTransaction tran, string cartonID, string whLoginID)
        {
            string strSql = this.SQL_CHECK_Carton_ID_UNIQUE.Replace("@_@", whLoginID) + " CARTON_ID = @CARTON_ID";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CARTON_ID", cartonID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, strSql, paras).ToString() == "1");
        }

        public bool CheckCartonUniqueByCartonGroup(string cartonGroup, string whLoginID)
        {
            string strSql = this.SQL_CHECK_Carton_ID_UNIQUE.Replace("@_@", whLoginID) + " CARTON_GROUP = @CARTON_GROUP";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CARTON_GROUP", cartonGroup) };
            return (Convert.ToInt32(DBHelper.ExecuteScalar(CommandType.Text, strSql, paras)) >= 1);
        }

        public bool CheckCartonUniqueByCartonGroup(DataBase dataBase, DbTransaction tran, string cartonGroup, string whLoginID)
        {
            string strSql = this.SQL_CHECK_Carton_ID_UNIQUE.Replace("@_@", whLoginID) + " CARTON_GROUP = @CARTON_GROUP";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CARTON_GROUP", cartonGroup) };
            return (Convert.ToInt32(dataBase.ExecuteScalar(tran, CommandType.Text, strSql, paras)) >= 1);
        }

        public bool CheckCartonUniqueByCartonType(DataBase dataBase, DbTransaction tran, string cartonType, string whLoginID)
        {
            string strSql = this.SQL_CHECK_Carton_ID_UNIQUE.Replace("@_@", whLoginID) + " CARTON_TYPE = @CARTON_TYPE";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CARTON_TYPE", cartonType) };
            return (Convert.ToInt32(dataBase.ExecuteScalar(tran, CommandType.Text, strSql, paras)) >= 1);
        }

        public int DeleteCarton(List<string> cartonID, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < cartonID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CARTON_ID", cartonID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_Carton.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteCarton(DataBase dataBase, DbTransaction tran, List<string> cartonID, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < cartonID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CARTON_ID", cartonID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_Carton.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllCarton(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_Carton.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllCarton(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_Carton.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public CartonInfo GetCartonByID(string cartonID, string whLogionID)
        {
            string sql = this.SQL_SELECT_ALL_Carton.Replace("@_@", whLogionID) + " where Carton_ID = @Carton_ID";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@Carton_ID", cartonID) };
            CartonInfo carton = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    carton = new CartonInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["Carton_ID"].ToString(), reader["CARTON_GROUP"].ToString(), reader["carton_type"].ToString(), reader["Description"].ToString(), (reader["Seq_no"].ToString() != "") ? int.Parse(reader["Seq_no"].ToString()) : 0, (reader["Cube"].ToString() != "") ? decimal.Parse(reader["Cube"].ToString()) : 0.0M, (reader["MAX_WEIGHT"].ToString() != "") ? decimal.Parse(reader["MAX_WEIGHT"].ToString()) : 0.0M, (reader["MAX_COUNT"].ToString() != "") ? decimal.Parse(reader["MAX_COUNT"].ToString()) : 0.0M, (reader["LENGTH"].ToString() != "") ? decimal.Parse(reader["LENGTH"].ToString()) : 0.0M, (reader["WIDTH"].ToString() != "") ? decimal.Parse(reader["WIDTH"].ToString()) : 0.0M, (reader["HEIGHT"].ToString() != "") ? decimal.Parse(reader["HEIGHT"].ToString()) : 0.0M, reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), reader["SYSTEM_CREATED"].ToString(), reader["IS_ACTIVE"].ToString());
                }
            }
            return carton;
        }

        public CartonInfo GetCartonByID(DataBase dataBase, DbTransaction tran, string cartonID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_Carton.Replace("@_@", whLoginID) + " where Carton_ID = @Carton_ID";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@Carton_ID", cartonID) };
            CartonInfo carton = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                carton = new CartonInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["Carton_ID"].ToString(), reader["CARTON_GROUP"].ToString(), reader["carton_type"].ToString(), reader["Description"].ToString(), (reader["Seq_no"].ToString() != "") ? int.Parse(reader["Seq_no"].ToString()) : 0, (reader["Cube"].ToString() != "") ? decimal.Parse(reader["Cube"].ToString()) : 0.0M, (reader["MAX_WEIGHT"].ToString() != "") ? decimal.Parse(reader["MAX_WEIGHT"].ToString()) : 0.0M, (reader["MAX_COUNT"].ToString() != "") ? decimal.Parse(reader["MAX_COUNT"].ToString()) : 0.0M, (reader["LENGTH"].ToString() != "") ? decimal.Parse(reader["LENGTH"].ToString()) : 0.0M, (reader["WIDTH"].ToString() != "") ? decimal.Parse(reader["WIDTH"].ToString()) : 0.0M, (reader["HEIGHT"].ToString() != "") ? decimal.Parse(reader["HEIGHT"].ToString()) : 0.0M, reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), reader["SYSTEM_CREATED"].ToString(), reader["IS_ACTIVE"].ToString());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return carton;
        }

        public DataSet GetCartonByQueryList(List<string> sqlWhere, CartonQueryEntity cartonQueryEntity, string whLogionID)
        {
            string temp = this.SQL_SELECT_ALL_Carton.Replace("@_@", whLogionID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!cartonQueryEntity.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, cartonQueryEntity.CurrentPage, cartonQueryEntity.PageSize, cartonQueryEntity.SortField, cartonQueryEntity.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetCartonByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, CartonQueryEntity cartonQueryEntity, string whLogionID)
        {
            string temp = this.SQL_SELECT_ALL_Carton.Replace("@_@", whLogionID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!cartonQueryEntity.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, cartonQueryEntity.CurrentPage, cartonQueryEntity.PageSize, cartonQueryEntity.SortField, cartonQueryEntity.SortDirection);
            }
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertCarton(CartonInfo cartonInfo, List<string> whLogionID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLogionID.Count; i++)
                {
                    cartonInfo.WhID = ConstUtils.CONST_DATABASE_PREFIX + whLogionID[i].Substring(0, whLogionID[i].Length - 1);
                    SqlParameter[] paras = this.Set_CartonInfo_Parameters(cartonInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_Carton.Replace("@_@", whLogionID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertCarton(DataBase dataBase, DbTransaction tran, CartonInfo cartonInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    cartonInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_CartonInfo_Parameters(cartonInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_Carton.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_CartonInfo_Parameters(CartonInfo carton)
        {
            SqlParameter[] paramArray = new SqlParameter[] { 
                new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.CARTON_ID, SqlDbType.VarChar, 20), new SqlParameter(this.CARTON_GROUP, SqlDbType.VarChar, 20), new SqlParameter(this.CARTON_TYPE, SqlDbType.VarChar, 20), new SqlParameter(this.DESCRIPTION, SqlDbType.VarChar, 100), new SqlParameter(this.SEQ_NO, SqlDbType.Int, 4), new SqlParameter(this.CUBE, SqlDbType.Float, 8), new SqlParameter(this.MAX_WEIGHT, SqlDbType.Float, 8), new SqlParameter(this.MAX_COUNT, SqlDbType.Decimal, 13), new SqlParameter(this.LENGTH, SqlDbType.Float, 8), new SqlParameter(this.WIDTH, SqlDbType.Float, 8), new SqlParameter(this.HEIGHT, SqlDbType.Float, 8), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.CREATED_DATE, SqlDbType.DateTime, 8), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_DATE, SqlDbType.DateTime, 8), 
                new SqlParameter(this.IS_ACTIVE, SqlDbType.VarChar, 1), new SqlParameter(this.SYSTEM_CREATED, SqlDbType.VarChar, 1)
             };
            if (!string.IsNullOrEmpty(carton.WhID))
            {
                paramArray[0].Value = carton.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = carton.CartonID;
            paramArray[2].Value = carton.CartonGroup;
            paramArray[3].Value = carton.CartonType;
            paramArray[4].Value = carton.Description;
            paramArray[5].Value = carton.SeqNo;
            paramArray[6].Value = carton.Cube;
            paramArray[7].Value = carton.MaxWeight;
            paramArray[8].Value = carton.MaxCount;
            paramArray[9].Value = carton.Length;
            paramArray[10].Value = carton.Width;
            paramArray[11].Value = carton.Height;
            paramArray[12].Value = carton.CreatedBy;
            paramArray[13].Value = DateTime.Now;
            paramArray[14].Value = carton.UpdatedBy;
            paramArray[15].Value = carton.UpdatedDate;
            paramArray[0x10].Value = carton.IsActive;
            paramArray[0x11].Value = carton.SystemCreated;
            return paramArray;
        }

        public int UpdateCarton(CartonInfo cartonInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    cartonInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_CartonInfo_Parameters(cartonInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_Carton.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateCarton(DataBase dataBase, DbTransaction tran, CartonInfo cartonInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    cartonInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_CartonInfo_Parameters(cartonInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_Carton.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

